package org.tobaccomain.service.serviceImpl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.tobaccomain.mapper.DeskMapper;
import org.tobaccomain.pojo.entity.Rfid;
import org.tobaccomain.service.UdpService;

import java.math.BigInteger;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class UdpServiceImpl implements UdpService {

    @Resource
    DeskMapper deskMapper;
    private static List<String> locations = new ArrayList<>();
    static {
        locations.add("demo");//1号实验室
        locations.add("1407");//2号实验室
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    @Async
    public void receiveData(String data, InetAddress address, int port) {

        // 处理接收到的数据，并存储到数据库
        data = data.substring(38);
        String[] tags = data.split("><l");
        //获取实验室名称
        String location = tags[0].split(" ")[3].substring(5, 9);
//        log.info("标签所在实验室信息:{}",location);
        //获取rfid信息
        List<Rfid> rfidList = new ArrayList<>();
        for (int i = 1; i < tags.length; i++) {
//            log.info("每一条标签信息:{}",tags[i]);
//            log.info("单条数据的各个信息如下:");
            String[] tagInfo = tags[i].split("><");
            String value = tagInfo[0].substring(9, 16);
            int x = getNumFromStr(tagInfo[1]);
            int y = getNumFromStr(tagInfo[2]);
//            log.info("rfid的x坐标:{}",x);
//            log.info("rfid的y坐标:{}",y);
            //获取rfid状态
            int state = getNumFromStr(tagInfo[3]);
            state = state & 1; // 直接获取状态的最低位
//            log.info("rfid状态:{}",state);
//            log.info("-----------------------------------------------------------");
            Rfid rfid = new Rfid();
            rfid.setValue(Long.valueOf(value));
            rfid.setX(x);
            rfid.setY(y);
            rfid.setState(state);
            rfid.setLocation(locations.indexOf(location)+1);
            rfidList.add(rfid);
        }
        //查看rfid的内容
//        for (int i = 0; i < rfidList.size(); i++) {
//            log.info("rfid = {}",rfidList.get(i));
//        }

        try {
            // 更新数据库
            for (int i = 0; i < rfidList.size(); i++) {
                deskMapper.updateRfid(rfidList.get(i));
            }
//            log.info("更新数据库.....");
        } catch (Exception e) {
            log.error("更新数据库时发生错误", e);
        }
    }

    //获取坐标信息
    private static int getNumFromStr(String str) {
        str = str.substring(2);
        StringBuilder stringBuilder = new StringBuilder();
        //获取字符串中的数字字符
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch >= '0' && ch <= '9'){
                stringBuilder.append(str.charAt(i));
            }
        }
        //将数字字符转化成数字
        int x = Integer.parseInt(stringBuilder.toString());
        return x;
    }
}
